package org.example.mall.admin.configuration.auth.check;

import cn.hutool.core.util.StrUtil;
import org.example.mall.admin.configuration.auth.code.Code;
import org.example.mall.admin.configuration.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;

/**
 * @author hzq
 * @date 2021/12/23 14:56
 */
public abstract class AbstractCheckAuthHandler implements CheckAuthHandler {

    @Autowired
    protected ValueOperations valueOperations;

    @Override
    public Object check(String scenes, String principal, String credentials) {

        if (StrUtil.isBlank(credentials)) {
            throw new BaseException("验证码不能为空");
        }

        // 获取验证码信息
        Object code = getCode(scenes, principal);
        if (code instanceof Code) {
            String strCode = ((Code) code).getCode();
            if (credentials.equals(strCode)) {
                return null;
            }
        }

        throw new BaseException("验证码信息有误");
    }

    /**
     * 获取验证码
     *
     * @param scenes    场景
     * @param principal 标识
     * @return 验证码
     */
    protected abstract Object getCode(String scenes, String principal);
}
